package com.ideal.manage.esms.compoment;

import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created By Dai On 2018/7/17
 * Remark: 每年1月30日0点删除去年的数据
 */
@Component
@Lazy(false)
public class DeleteOneYearAgoComponent {
    @Autowired
    private TransportClient transportClient;

    private static TransportClient client;

    @PostConstruct
    public void init() {
        client = this.transportClient;
    }

    /**
     * 每年1月30日0点删除去年的数据
     *
    @Scheduled(cron = "0 0 0 30 1 ?")
     */

    public void getAlarmHits(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -1);
        String index = "mdbs-"+simpleDateFormat.format(calendar.getTime())+"*";
        DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(index)
                .execute().actionGet();
    }
}
